package com.taotao.common.model;

import com.alibaba.fastjson2.JSON;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * ajax 返回给前端的数据 模型
 */
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RespData<T> {

    /**
     * 成功
     */
    public static final int SUCCEED_CODE = 1;
    /**
     * 失败
     */
    public static final int FAIL_CODE = 0;

    /**
     * 重新登录
     */
    public static final int RE_LOGIN_CODE = -1;

    /**
     * 状态码
     */
    @ApiModelProperty(value = "状态码；1-成功，0-失败，-1-重新登录")
    private int code;

    /**
     * 错误描述
     */
    @ApiModelProperty(value = "错误描述")
    private String msg;

    /**
     * 调试的提示
     */
    private String debugInfo;

    /**
     * 返回数据
     */
    @ApiModelProperty(value = "返回数据")
    private T data;


    public RespData(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public boolean isSuccess() {
        return SUCCEED_CODE == code;
    }

    public boolean isFailed() {
        return FAIL_CODE == code;
    }

    public boolean isReLogin() {
        return RE_LOGIN_CODE == code;
    }


    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public static <E> RespData<E> successResp() {
        return new RespData<E>(SUCCEED_CODE, null, null);
    }


    public static <E> RespData<E> successResp(E data) {
        return new RespData<E>(SUCCEED_CODE, null, data);
    }

    public static <E> RespData<E> failedResp(String msg) {
        return new RespData<E>(FAIL_CODE, msg, null);
    }


    public static <E> RespData<E> reLoginResp() {
        return new RespData<E>(RE_LOGIN_CODE, "请重新登录", null);
    }


}